package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import pers.cj.pdos.cache.*;
import pers.cj.pdos.constant.CacheConstant;
import pers.cj.pdos.constant.Constant;
import pers.cj.pdos.entity.CustomType;
import pers.cj.pdos.entity.Role;
import pers.cj.pdos.entity.RolePermission;
import pers.cj.pdos.entity.UserRole;
import pers.cj.pdos.mapper.RoleMapper;
import pers.cj.pdos.mapper.RolePermissionMapper;
import pers.cj.pdos.mapper.UserRoleMapper;
import pers.cj.pdos.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Role> listPage(String companyId, Integer page, Integer limit) {

        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);

        if (page !=null && limit != null) {
            IPage<Role> iPage = roleMapper.selectPage(new Page<>(page,limit), wrapper);
            return iPage.getRecords();
        } else {
            return roleMapper.selectList(wrapper);
        }
    }

    @Override
    public long count(String companyId) {
        return roleMapper.selectCount(new QueryWrapper<Role>().eq("company_id",companyId));
    }

    @Override
    public boolean checkName(String name, String companyId) {
        return roleMapper.selectCount(new QueryWrapper<Role>().eq("company_id",companyId).eq("name",name)) > 0;
    }

    @Override
    @CacheData(clazz = RoleServiceImpl.class, param = "#companyId")
    public List<Role> listWithoutAdmin(String companyId) {
        return roleMapper.selectList(new QueryWrapper<Role>()
                .eq("company_id",companyId)
                .ne("name","管理员")
                .orderByDesc("time")
        );
    }

    @Override
    @CacheRemove(clazz = RoleServiceImpl.class,methodName = "listWithoutAdmin",param = "#role.companyId")
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    @CacheRemoveMore(cacheRemoves = {
            @CacheRemove(clazz = RoleServiceImpl.class,methodName = "listWithoutAdmin",param = "#role.companyId"),
            @CacheRemove(clazz = RolePermissionServiceImpl.class,methodName = "listByRole", param = "#role.id"),
            @CacheRemove(clazz = RoleServiceImpl.class, methodName = "getOne", param = "#role.id")
    })
    @CacheRemovePermission(roleId = "#role.id")
    public void remove(Role role) {
        //删除角色之前，要先删除角色权限表和用户角色表
        userRoleMapper.delete(new QueryWrapper<UserRole>().eq("role_id",role.getId()));
        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id",role.getId()));
        roleMapper.deleteById(role.getId());
    }

    @Override
    @CacheRemoveMore(cacheRemoves = {
            @CacheRemove(clazz = RoleServiceImpl.class,methodName = "listWithoutAdmin",param = "#role.companyId"),
            @CacheRemove(clazz = RoleServiceImpl.class, methodName = "getOne", param = "#role.id")
    })
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    @CacheData(clazz = RoleServiceImpl.class, param = "#id")
    public Role getOne(String id) {
        return roleMapper.selectById(id);
    }
}
